#!/bin/bash
#系统初始化,安装kubernetes

#1查找我的函数文件
search_public_functions() {
	#输入函数文件名称
	functions_name="public_functions"
	#获取函数文件名称路径
	functions_url="$(sudo find / -path /proc -prune -o -name ${functions_name} -print)"
	#查找函数文件并加载
	if [ -n "${functions_url}" ]; then
        	for i in "${functions_url}"; do
			#加载文件
		       	. $i
		done
	else
        	echo "Not fount ${functions_name}"
		exit 1
	fi
}

#2获取系统类型
get_distribution() {
        lsb_dist=""
        # Every system that we officially support has /etc/os-release
        if [ -r /etc/os-release ]; then
                lsb_dist="$(. /etc/os-release && echo "$ID")"
        fi
        # Returning an empty string here should be alright since the
        # case statements don't act unless you provide an actual value
        echo "${lsb_dist}"
}

#3关闭swap
swap_disable() {
	#命令行关闭swap
	sudo swapoff --all
	#文件关闭swap
	if [ -f /etc/fstab ]; then
        	sudo sed -i '/.*\/dev.*swap/ {
			/^#/!s/^/#/
		}' /etc/fstab
	fi
	#ubuntu系统的额外操作,服务关闭swap
	if [ "$(get_distribution)" == "ubuntu" ]; then
		sudo systemctl --type swap
		sudo systemctl mask SWAP_DEV
	fi
}

#4关闭selinux
selinux_disable() {
	#获取selinux状态
	selinuxStatus=$(getenforce)
	if [ "${selinuxStatus}" == 'Disabled' ]; then
		logW "selinux已经是关闭状态\n"
	else
		setenforce 0 &>/dev/null
		echo
	fi
	sudo sed -Ei "s/SELINUX=(enforcing|permissive)/SELINUX=disabled/" /etc/selinux/config &>/dev/null
}

#5关闭防火墙
firewalld_disable() {
	#centos7系统
	if [ "$(get_distribution)" == "centos" ]; then
		#检测防火墙运行状态
		firewall-cmd --state -q
		if [[ $? -eq 0 ]]; then
			systemctl disable --now firewalld
		fi
	fi
	#ubuntu系统
	if [ "$(get_distribution)" == "ubuntu" ]; then
		#检测防火墙运行状态
		ufw status &>/dev/null
		if [[ $? -eq 0 ]]; then
			sudo ufw disable &>/dev/null
		fi
	fi
}

#6设定时钟同步
chrony_enable() {
	case "$(get_distribution)" in
		centos)
			yum clean all
			yum install chrony -y
			#注释自带时间服务器地址，添加阿里云时间服务器地址
			sed  -i '/^server.*centos.*iburst/ {
				/^#/!s/^/#/
				/0/i server ntp1.aliyun.com iburst
			}' /etc/chrony.conf
			systemctl enable --now chronyd
			systemctl restart chronyd
			timedatectl set-timezone Asia/Shanghai
		;;
		ubuntu)
			sudo apt install chrony -y
			#注释自带时间服务器地址，添加阿里云时间服务器地址
			sudo sed  -i '/^pool.*ubuntu.*iburst/ {
				/^#/!s/^/#/
				/0/i pool ntp1.aliyun.com iburst
			}' /etc/chrony/chrony.conf
			sudo systemctl enable --now chrony
			sudo systemctl restart chronyd
			sudo timedatectl set-timezone Asia/Shanghai
		;;
	esac
}

search_public_functions		#1查找我的函数文件
#get_distribution		#2获取系统名称
swap_disable			#3关闭swap
selinux_disable			#4关闭selinux
firewalld_disable		#5关闭防火墙
chrony_enable			#6设定时钟同步


#---------安装docker-ce---------------------------------------------------------------------------
sudo sh install-docker.sh --version 24.0 --mirror Aliyun

#readonly host_ip=$(hostname -I | cut -d" " -f1)
cat <<-EOF | sudo tee /etc/docker/daemon.json 
	{
	"registry-mirrors": ["https://registry.docker-cn.com"],
	"exec-opts": ["native.cgroupdriver=systemd"],
	"log-driver": "json-file",
	"storage-driver": "overlay2",
	"log-opts": {
	  "max-size": "200m"
	  },
	"insecure-registries": ["127.0.0.1:5000","registry:5000"]
	}
	EOF
	sudo systemctl restart docker.service

##本地导入registry镜像,启动registry容器
#if [ -f ./kube_container_v1.31.0/registry.2.8.tar ]; then
#	sudo docker load -i ./kube_container_v1.31.0/registry.2.8.tar
#	sudo docker run -d -p 5000:5000 --restart always --name registry registry:2.8
#fi

##导入k8s所需镜像
#readonly docker_images=$(ls kube_container_v1.31.0)
#for i in $docker_images
#do
#	docker load -i kube_container_v1.31.0/$i
#done

##获取镜像repositry名称和tag
#readonly docker_image_repositry_tag=$(docker images --format "{{.Repository}}:{{.Tag}}")
##确定镜像仓库ip地址
#readonly images_repo_ip="192.168.38.133"
##为镜像打标签并上传到本地仓库
#for i in $docker_image_repositry_tag
#do
#	if [ $i == 'registry:2.8' ]; then
#		continue
#	fi
#
#	if [ $i == 'registry.k8s.io/coredns/coredns:v1.11.1' ]; then
#		image_name=$(echo $i | cut -d "/" -f3)
#	else
#		image_name=$(echo $i | cut -d "/" -f2)
#	fi
#
#	docker tag $i ${images_repo_ip}:5000/$image_name
#	docker push ${images_repo_ip}:5000/$image_name
#	docker rmi $i
#done

